#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>

void main(int argc, char * argv[])
{
	pid_t childPID;
	pid_t waitPID;
	int status;

	/* fork-Spielerei */
	switch(childPID = fork())
	{
		case -1:
			/* error */
			fprintf(stderr, "error in fork\n");
			exit(0);
			break;
		case 0:
			/* child process */
			printf("CHILD: My PID: %d\n", getpid());
			printf("CHILD: My Parent's PID: %d\n", getppid());
			printf("CHILD: Value of my copy of pid: %d\n", childPID);
			printf("CHILD: Goodbye!\n");
			exit(0);
			break;
		default:
			/* parent process */
			printf("PARENT: My PID: %d\n", getppid());
			printf("PARENT: Value of my copy of pid: %d\n", getpid());
			printf("PARENT: Waiting for my child to exit.\n");
			/* status: exit-status of child; 0: option*/
			while((waitPID = waitpid(childPID, &status, 0)) != childPID)
			{
				/* waitpid returns -1 if error */
				if(waitPID == -1)
				{
					if(errno == EINTR)
					{
						continue;
					}

					printf("error waitpid");
				}
			}

			/* WEXITSTATUS: Extraction of status of child's exit */
			if(WIFEXITED(status))
			printf("PARENT: Child's exit status: %d\n", WEXITSTATUS(status));
			printf("PARENT: Goodbye!\n");
			exit(0);
			break;
		
	}
}

